/**
 * 服务器管理控制器
 * 提供服务器列表、状态查询等API接口
 * 
 * @author mixed-project
 * @version 1.0
 * @since 2024
 */
package com.mixed.mixedserver.controller;

import com.mixed.mixedserver.game.network.GameNettyServer;
import com.mixed.mixedserver.game.network.GameServerHandler;
import com.mixed.mixedserver.service.ServerService;
import jakarta.annotation.Resource;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/server")
@CrossOrigin(origins = "*")
public class ServerController {
    
    @Resource
    private ServerService serverService;
    
    @Resource
    private GameNettyServer gameNettyServer;
    
    @Resource
    private GameServerHandler gameServerHandler;
    
    /**
     * 获取服务器列表
     * 返回可用的游戏服务器列表及其状态信息
     * 
     * @return 服务器列表
     */
    @GetMapping("/list")
    public ResponseEntity<List<Map<String, Object>>> getServerList() {
        List<Map<String, Object>> serverList = serverService.getAvailableServers();
        return ResponseEntity.ok(serverList);
    }
    
    /**
     * 获取特定服务器详细信息
     * 
     * @param serverId 服务器ID
     * @return 服务器详细信息
     */
    @GetMapping("/info/{serverId}")
    public ResponseEntity<Map<String, Object>> getServerInfo(@PathVariable String serverId) {
        Map<String, Object> serverInfo = serverService.getServerInfo(serverId);
        if (serverInfo != null) {
            return ResponseEntity.ok(serverInfo);
        } else {
            return ResponseEntity.notFound().build();
        }
    }
    
    /**
     * 获取当前服务器状态
     * 包括在线人数、服务器负载等信息
     * 
     * @return 当前服务器状态
     */
    @GetMapping("/status")
    public ResponseEntity<Map<String, Object>> getCurrentServerStatus() {
        Map<String, Object> status = serverService.getCurrentServerStatus();
        return ResponseEntity.ok(status);
    }
    
    /**
     * 刷新服务器列表
     * 重新扫描并更新可用服务器信息
     * 
     * @return 刷新结果
     */
    @PostMapping("/refresh")
    public ResponseEntity<Map<String, Object>> refreshServerList() {
        try {
            serverService.refreshServerList();
            return ResponseEntity.ok(Map.of(
                "status", "success",
                "message", "服务器列表已刷新",
                "timestamp", System.currentTimeMillis()
            ));
        } catch (Exception e) {
            return ResponseEntity.status(500).body(Map.of(
                "status", "error",
                "message", "刷新失败: " + e.getMessage(),
                "timestamp", System.currentTimeMillis()
            ));
        }
    }
    
    /**
     * 检查服务器连接
     * 测试与指定服务器的连接状态
     * 
     * @param serverId 服务器ID
     * @return 连接测试结果
     */
    @PostMapping("/ping/{serverId}")
    public ResponseEntity<Map<String, Object>> pingServer(@PathVariable String serverId) {
        Map<String, Object> pingResult = serverService.pingServer(serverId);
        return ResponseEntity.ok(pingResult);
    }
    
    /**
     * 获取服务器统计信息
     * 包括历史在线人数、服务器性能等统计数据
     * 
     * @return 服务器统计信息
     */
    @GetMapping("/statistics")
    public ResponseEntity<Map<String, Object>> getServerStatistics() {
        Map<String, Object> statistics = serverService.getServerStatistics();
        return ResponseEntity.ok(statistics);
    }
}